package com.example.pixelyear.Interface;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pixelyear.adapter.BaseAdapter;
import com.socks.library.KLog;



/**
 * Package    : com.example.aliplay
 * ClassName  : Touch
 * Description: ${TODO}
 * Date       : 2019/4/17 17:30
 */
public class Touch extends ItemTouchHelper.Callback {

    private BaseAdapter mAdapter;

    public Touch(BaseAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
                ItemTouchHelper.LEFT);

    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        mAdapter.onMove(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition());
        return true;
    }

//    @Override
//    public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
//        super.onSelectedChanged(viewHolder, actionState);
//        if (viewHolder != null && actionState != ACTION_STATE_IDLE) {
//            // 非闲置状态下，记录下起始 position
//            startPosition = viewHolder.getAdapterPosition();
//        }
//        if (actionState == ACTION_STATE_IDLE) {
//            // 当手势抬起时刷新，endPosition 是在 onMove() 回调中记录下来的
//            if (mAdapter != null) {
//                mAdapter.notifyItemRangeChanged(Math.min(startPosition, endPosition), Math.abs(startPosition - endPosition) + 1);
//            }
//        }
//    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        KLog.e("yxs", "Touch删除：" + viewHolder.getAdapterPosition() + "===" + i);
        mAdapter.onRemover(viewHolder.getAdapterPosition());
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return mAdapter.getCanSwipe();
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return mAdapter.getCanDrag();
    }


}
